<?php
namespace Common\ORG\Qy;

/**
 * 通讯录部门管理 
 * 
*/

use \Common\ORG\Tools\Http;

class Department extends Base{
    const URL_QUERY = 'https://qyapi.weixin.qq.com/cgi-bin/department/list';
    const URL_CREATE = 'https://qyapi.weixin.qq.com/cgi-bin/department/create';
    const URL_UPDATE = 'https://qyapi.weixin.qq.com/cgi-bin/department/update';
    const URL_DELETE = 'https://qyapi.weixin.qq.com/cgi-bin/department/delete';


    public function __construct($token)
    {
        $this->_token = $token;
    }

    /**
     * 创建部门
     * 
     * @param string $name 部门名称
     * @param int    $parentid 上级部门ID
     * @param int    $order     排序，为空则
     * @param int    $id        设置部门的ID 
     * 
     * @return mixed array('rcode'=>0/1 'msg'=>'','data'=>)
    */
    public function create($name, $parentid, $id = null, $order = null)
    {
        $ret = $this->m_ret;   
        $url = self::URL_CREATE.'?access_token='.urlencode($this->_token);
        $data = array('name'=>$name, 'parentid'=>$parentid );
        if ($id)
            $data['id'] = $id;
        if ($order)
            $data['order'] = $order;
        if ($id &&$id<2){
            $res['msg'] = '部门ID必须大于1';
            return $res;
        }
        $param = $this->_param($data);
        $res = Http::post($url, $param);
        $crackData = $this->crackRes( $res );
        $ret['rcode'] = $crackData['rcode'];
        if ($ret['rcode'])
            $ret['msg'] = 'OK';
        else {
            $ret['msg'] = $ret['msg'] = $crackData['msg'];
        }
        return $ret;
    }

    /**
     * 更新通讯录部门节点
     * 
     * @param int $id   部门ID 
     * @param string $name 部门名字
     * @param int  $parentid  部门的上级部门
     * @param int   部门在父级部门的排序
     * 
     * @return mixed  array('rcode'=>0/1,'msg'=>'',)
    */
    public function update($id, $name, $parentid = null, $order=null)
    {
        $ret = $this->m_ret;
        $url = self::URL_UPDATE.'?access_token='.urldecode( $this->_token);
        $data = array('id'=>$id, 'name'=>$name);
        if ($parentid&&$parentid>0){
            $data['parentid'] = $parentid;
        }
        if ($order){
            $data['order'] = $order;
        }
        $param = $this->_param($data);
        $str = http::post($url, $param);
        $crackData = $this->crackRes( $str );
        $ret['rcode'] = $crackData['rcode'];
        $ret['msg'] = $crackData['rcode'] ? 'OK':$crackData['msg'];
        return $ret;
    }

    /**
     * 删除通讯录节点
     * 
     * @param int $id 部门ID
     * 
     * @return  mixed array('rcode'=>0/1,'msg'=>'')
    */
    public function delete( $id = null )
    {
        $ret = $this->m_ret;
        if ( $id && $id<2 ){
            $ret['msg'] = '部门节点不能删除';
        }else{
            $param = array();
            $param['access_token'] = $this->_token;
            if($id)   $param['id'] = $id;
            $url = self::URL_DELETE.'?'.http_build_query( $param );
            $str = http::get( $url );
            $crackData = $this->crackRes( $str );
            $ret['rcode'] = $crackData['rcode'];
            $ret['msg'] = $crackData['rcode'] ? 'OK': $crackData['msg'];
        }

        return $ret;
    }

    /**
     * 查询部门下的子部门
     * 
     * @param int $depId 查询的部门，默认是根节点：1
     * 
     * @return array(
     *                  'rcode'=>0/1, 接口调用结果
     *                  'msg' => '提示信息',
     *                  'data' => '部门数据',
     *              )
    */
    public function query( $depId = 1)
    {
        $ret = $this->m_ret;
        $param = array('access_token'=>'');
        $param['access_token'] = $this->_token;
        if ($depId)
            $param['id'] = $depId;
        $url = self::URL_QUERY.'?'.http_build_query($param);
        $res = Http::get($url);
        $crackData = $this->crackRes( $res );
        $ret['rcode'] = $crackData['rcode'];
        if ($crackData['rcode']&&isset($crackData['data'])){
            $ret['msg'] = 'OK';
            $ret['data'] = isset($crackData['data']['department']) ? $crackData['data']['department']:false;
        }
        else
            $ret['msg'] = $crackData['msg'];
        return $ret;
    }
}


?>